﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.kmer.Helper;
using com.kmer.library.Helper;

namespace TouchUIWeiXinAPI.common
{
    public static class WXQiYeHelper
    {
        public static WXAccessToken GetWXAccessToken()
        {
            var json = GetHelper.HttpGetData(
                $@"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={QyWxConfit.CorpId}&corpsecret={QyWxConfit.CorpSecret}");
            var result = json.FromJson<WXAccessToken>();
            return result;
        }

        public static WXSessionKey GetWXSessionKey(string Code,string AccessToken)
        {
            var json = GetHelper.HttpGetData($@"https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token={AccessToken}&js_code={Code}&grant_type=authorization_code");
            var result = json.FromJson<WXSessionKey>();
            return result;
        }
    }


    public class WXResult
    {
        public string errcode { get; set; }
        public string errmsg { get; set; }
    }

    public class WXAccessToken : WXResult
    {
        /// <summary>
        /// 获取到的凭证,最长为512字节
        /// </summary>
        public string access_token { get; set; }
        /// <summary>
        /// 凭证的有效时间（秒）
        /// </summary>
        public int expires_in { get; set; }
    }

    public class WXSessionKey : WXResult
    {
        /// <summary>
        /// 用户所属企业的corpid
        /// </summary>
        public string corpid { get; set; }
        /// <summary>
        /// 用户在企业内的UserID，对应管理端的帐号，企业内唯一。注意：如果该企业没有关联该小程序，则此处返回加密的userid
        /// </summary>
        public string userid { get; set; }
        /// <summary>
        /// 会话密钥
        /// </summary>
        public string session_key { get; set; }
    }
}
